<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>18绘制一个圆分成六等分颜色随机</title>
</head>
<style>
    canvas {
        border: 1px solid #ccc;
    }
</style>
<body>
<canvas width="600" height="400"></canvas>
<script>
    var myCanvas = document.querySelector('canvas');
    var ctx = myCanvas.getContext('2d');
    /*18绘制一个圆分成六等分颜色随机*/
    var w = ctx.canvas.width;
    var h = ctx.canvas.height;
    /*分成几等分*/
    var num = 6;
    /*一份多少弧度*/
    var angle = Math.PI * 2 / num;
    /*原点坐标*/
    var x0 = w / 2;
    var y0 = h / 2;
    /*半径的长度*/
    var r = 150;
    /*获取随机颜色*/
    var getRandomColor = function () {
        var i = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return 'rgb(' + i + ',' + g + ',' + b + ')';
    };
    /*上一次绘制的结束弧度等于当前次的起始弧度*/
    var startAngle = 0;
    var endAngele = 0;
    for (var i = 0; i < num; i++) {
        console.log(i);
        startAngle = i * angle;
        endAngele = (i + 1) * angle;
        ctx.beginPath();
        ctx.moveTo(x0, y0);
        ctx.arc(x0, y0, r, startAngle, endAngele);
        /*随机颜色*/
        ctx.fillStyle = getRandomColor();
        ctx.fill();
    }

</script>
</body>
</html>
